package com.xingmang.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingmang.common.constant.PageEntity;
import com.xingmang.common.constant.ResponseData;
import com.xingmang.common.constant.ResultBean;
import com.xingmang.common.dto.admin.ThirdAppInfoDTO;
import com.xingmang.common.dto.admin.ThirdAppPageDTO;
import com.xingmang.common.dto.admin.ThirdAppSaveDTO;
import com.xingmang.common.dto.admin.ThirdAppUpdateDTO;
import com.xingmang.common.utils.TokenUtils;
import com.xingmang.common.vo.admin.ThirdAppPageVO;
import com.xingmang.db.entity.ThirdApp;
import com.xingmang.db.entity.ThirdCategory;
import com.xingmang.db.service.ThirdAppDBService;
import com.xingmang.db.service.ThirdCategoryDBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: hansom
 * @Version: 1.0
 * @create 2023/6/19 15:56
 */
@Service
@Slf4j
public class ThirdAppService {

    @Resource
    private ThirdAppDBService thirdAppDBService;

    @Resource
    private ThirdCategoryDBService thirdCategoryDBService;


    public ResultBean<PageEntity<ThirdAppPageVO>> page(ThirdAppPageDTO dto) {
        Page<ThirdApp> page = thirdAppDBService.page(new Page<>(dto.getPagenum(), dto.getPagesize()),
                new LambdaQueryWrapper<ThirdApp>()
                        .like(StringUtils.isNotEmpty(dto.getAppName()), ThirdApp::getAppName, dto.getAppName())
                        .eq(dto.getCategoryId() != null, ThirdApp::getCategoryId, dto.getCategoryId())
                        .orderByDesc(ThirdApp::getCreateTime));
        List<ThirdAppPageVO> collect = page.getRecords().stream().map(thirdApp -> {
            ThirdAppPageVO vo = BeanUtil.copyProperties(thirdApp, ThirdAppPageVO.class);
            vo.setThirdAppId(thirdApp.getId());
            ThirdCategory category = thirdCategoryDBService.getById(thirdApp.getCategoryId());
            if (category != null) {
                vo.setCategoryName(category.getCategoryName());
            }
            return vo;
        }).collect(Collectors.toList());
        PageEntity<ThirdAppPageVO> pageEntity = new PageEntity<>();
        BeanUtil.copyProperties(page, pageEntity);
        pageEntity.setRecords(collect);
        return ResponseData.success(pageEntity);
    }

    public ResultBean save(ThirdAppSaveDTO dto) {
        Long userId = TokenUtils.getUserId();
//        Long userId = 1L;
        ThirdCategory thirdCategory = thirdCategoryDBService.getById(dto.getCategoryId());
        if (thirdCategory==null){
            return ResponseData.error("选择的应用分类已不存在");
        }else{//创建新的应用，需要在对应的应用分类下增加应用数量
            ThirdApp one = thirdAppDBService.getOne(new LambdaQueryWrapper<ThirdApp>().eq(ThirdApp::getAppName, dto.getAppName()).eq(ThirdApp::getCategoryId, dto.getCategoryId()));
            if (one!=null){
                return ResponseData.error("此分类下的应用名已存在");
            }
            ThirdApp thirdApp = BeanUtil.copyProperties(dto, ThirdApp.class);
            thirdApp.setUserId(userId);
            thirdAppDBService.save(thirdApp);
            thirdCategory.setThirdCount(thirdCategory.getThirdCount()+1);
            thirdCategoryDBService.updateById(thirdCategory);
        }
        return ResponseData.success(null);
    }

    public ResultBean update(ThirdAppUpdateDTO dto) {
        ThirdApp thirdApp = BeanUtil.copyProperties(dto, ThirdApp.class);
        thirdApp.setId(dto.getThirdAppId());
        thirdAppDBService.updateById(thirdApp);
        return ResponseData.success(null);
    }

    public ResultBean delete(ThirdAppInfoDTO dto) {
        ThirdApp thirdApp = thirdAppDBService.getById(dto.getThirdAppId());
        thirdAppDBService.removeById(dto.getThirdAppId());
        ThirdCategory thirdCategory = thirdCategoryDBService.getById(thirdApp.getCategoryId());
        thirdCategory.setId(thirdApp.getCategoryId());
        thirdCategory.setThirdCount(thirdCategory.getThirdCount()-1);
        thirdCategoryDBService.updateById(thirdCategory);
        return ResponseData.success(null);
    }
}
